package com.wyc.controller;


import com.myaliyun.oss.AliOSSUtils;
import com.wyc.pojo.Result;
//import com.wyc.utils.AliOSSProperties;
//import com.wyc.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
public class UploadController {


//    @Autowired
//    private AliOSSProperties aliOSSProperties;

    @Autowired(required = false)
    private AliOSSUtils aliOSSUtils;


//    aliyun.oss.wyc.accesKeyId=LTAI5tEe6nWFZHuo6xCsN7QQ
//    aliyun.oss.wyc.accessKeySecret=J16urr4fxAMTBZ87AGHXtoZXtBiRv8
//    aliyun.oss.wyc.endpoint=oss-cn-hangzhou.aliyuncs.com
//    aliyun.oss.wyc.bucketName=wangyongcheng

//    @Value("${aliyun.oss.wyc.accesKeyId}")
//    private String accesKeyId;
//    @Value("${aliyun.oss.wyc.accessKeySecret}")
//    private String accessKeySecret;
//    @Value("${aliyun.oss.wyc.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.wyc.bucketName}")
//    private String bucketName;



    @PostMapping("/upload")
    public Result upload(MultipartFile multipartFile) {

        int index = multipartFile.getOriginalFilename().lastIndexOf(".");
        String extname = multipartFile.getOriginalFilename().substring(index);
        String newFileName= UUID.randomUUID().toString()+extname;

//        String accesKeyId = aliOSSProperties.getAccesKeyId();
//        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
//        String bucketName = aliOSSProperties.getBucketName();
//        String endpoint = aliOSSProperties.getEndpoint();


        String imageName=newFileName;
//        String savePath="wyc";
//        Long fileSize=multipartFile.getSize();
        InputStream fileInputStream;
        try {
            fileInputStream=multipartFile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            //上传
//            aliOSSUtils.uploadFile(accesKeyId,accessKeySecret,endpoint,bucketName,imageName,fileInputStream);
            aliOSSUtils.uploadFile(imageName,fileInputStream);
            //获取上传成功后的图片url
//            String visitUrl = aliOSSUtils.getVisitUrl(accesKeyId, accessKeySecret, endpoint, bucketName, imageName);
            String visitUrl = aliOSSUtils.getVisitUrl(imageName);

            return Result.success(visitUrl);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
